package com.apkfuns.demo.slice;

import com.apkfuns.demo.utils.ThreadLog;
import com.apkfuns.logutils.LogUtils;
import com.apkfuns.logutils.ResourceTable;
import com.apkfuns.demo.helper.DataHelper;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;

public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener{
    String[] permission = new String[]{
            "ohos.permission.WRITE_USER_STORAGE",
            "ohos.permission.READ_USER_STORAGE",
    };
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        requestPermissionsFromUser(permission,0);
        ComponentContainer container = (ComponentContainer) findComponentById(ResourceTable.Id_container);
        for (int i = 0; i < container.getChildCount(); i++) {
            container.getComponentAt(i).setTag(i);
            container.getComponentAt(i).setClickedListener(this);
        }
        // 进行多进程测试
        Operation operation = new Intent.OperationBuilder()
                .withDeviceId("")
                .withBundleName("com.apkfuns.demo.slice")
                .withAbilityName("com.apkfuns.demo.service")
                .build();
        intent.setOperation(operation);
        startAbility(intent);
    }


    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onClick(Component component) {
        switch (((int) component.getTag())) {
            case 0:
                logString();
                break;
            case 1:
                logBigString();
                break;
            case 2:
                logThred();
                break;
            case 3:
                flushAsync();
                break;
            case 4:
                logIntent();
                break;
            default:
                break;
        }
    }


    private void logString() {
        LogUtils.d("12345!!");
    }

    private void logBigString() {
        LogUtils.i(DataHelper.getBigString(this));
    }

    private void flushAsync() {
        LogUtils.getLog2FileConfig().flushAsync();
    }

    private void logThred() {
        new ThreadLog("thread1", "msg from 1").start();
        new ThreadLog("thread2", "msg from 2").start();
        new ThreadLog("thread3", "msg from 3").start();
    }

    private void logIntent() {
        Intent intent = new Intent();
        Operation operation = new Intent.OperationBuilder()
                .withDeviceId("")
                .withBundleName("com.apkfuns.demo.slice")
                .withAbilityName("com.apkfuns.demo.service.MainAbility")
                .build();
        intent.setOperation(operation);
        startAbility(intent);
        intent.addFlags(Intent.FLAG_ABILITY_NEW_MISSION);
        LogUtils.i(intent);
    }


}
